﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace T3K.SketchEditor.Core {
    public partial class NewFileForm : Form {

        private FileTemplate _template;

        public FileTemplate Template {
            get { return _template; }
        }

        public string FileName {
            get { return this.txtName.Text; }
        }

        public NewFileForm() {
            InitializeComponent();
            lblDescription.Text = string.Empty;

            List<FileTemplate> templates = FileTemplate.GetTemplates();

            ImageList list = new ImageList();
            list.ImageSize = new Size(64, 64);

            listTemplates.LargeImageList = list;

            for (int i = 0; i < templates.Count; i++) {
                if (templates[i].Icon != null && templates[i].Icon.Trim() != string.Empty) {
                    Image img = EditorManager.Instance.GetImage(templates[i].Icon);
                    if (img != null) {
                        list.Images.Add(templates[i].Icon, img);
                    }
                }       
            }

            for (int i = 0; i < templates.Count; i++) {
                if (templates[i].Public) {
                    FileTemplate tmpl = templates[i];
                    ListViewItem item = new ListViewItem(tmpl.Name, 0);
                    item.Tag = tmpl;
                    listTemplates.Items.Add(item);
                    listTemplates.Items[i].ImageKey = templates[i].Icon;
                }
            }
            

        }

        private void NewFileForm_Load(object sender, EventArgs e) {

        }

        private void listTemplates_SelectedIndexChanged(object sender, EventArgs e) {
            if (listTemplates.SelectedItems.Count >= 1) {
                FileTemplate tmpl = listTemplates.SelectedItems[0].Tag as FileTemplate;
                if (tmpl != null) {
                    lblDescription.Text = tmpl.Description;
                    _template = tmpl;
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e) {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }

        private void btnOk_Click(object sender, EventArgs e) {

            if (this.txtName.Text.Trim() == string.Empty) {
                errorProvider.SetError(txtName, "Please provide the filename.");
                return;
            }

            if (_template == null) {
                errorProvider.SetError(txtName, "Please select a file type (template)");
                return;
            }

            if (!txtName.Text.EndsWith(_template.Extention))
                txtName.Text += _template.Extention;

            errorProvider.SetError(txtName, "");
            this.DialogResult = DialogResult.OK;
            this.Close();
        }
    }
}
